public class test6 {
    /**
     * 最长回文字串：
     * 给你一个字符串 s，找到 s 中最长的回文子串
     *示例：
     * 输入：s = "babad"
     * 输出："bab"
     * 解释："aba" 同样是符合题意的答案。
     */
    public static void main(String[] args) {
        String s = "aacabdkacaa";
        String ret = longestPalindrome(s);
        System.out.println(ret);
    }

    public static String longestPalindrome(String s) {
        String tmp = null;
        String tmp1 = null;
        String ret = null;
        int retLen = 0;
        for (int i = 0; i < s.length(); i++) {
            tmp1 = null;
            tmp = String.valueOf(s.charAt(i));
            for (int j = i + 1; j < s.length(); j++) {
                if (s.charAt(i) != s.charAt(j)) {
                    tmp = tmp.concat(String.valueOf(s.charAt(j)));
                } else {
                    tmp = tmp.concat(String.valueOf(s.charAt(j)));
                    tmp1 = tmp;
                }
            }
            if (tmp1 != null) {
                int fast = 0;
                int last = tmp1.length() - 1;
                while (fast < last && tmp1.charAt(fast) == tmp1.charAt(last)) {
                    if (fast + 1 == last) {
                        fast++;
                    } else {
                        fast++;
                        last--;
                    }
                }
                if (fast == last && tmp1.length() > retLen) {
                    ret = tmp1;
                    retLen = tmp1.length();
                }
            }
        }
        if (ret == null && s != null) {
            ret = String.valueOf(s.charAt(0));
        }
        return ret;
    }
}
